//
//  YBAFNetWorkingTool.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBAFNetWorkingTool.h"

@implementation YBAFNetWorkingTool
+ (void)postWithUrlString:(NSString *)url Parameter:(NSDictionary *)parameter Success:(NetBlock)successBlock Fail:(NetBlock)failBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //设置网络请求超时时间
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript",nil];
    NSString * sessionString = [[NSUserDefaults standardUserDefaults] objectForKey:@"sessionId"];
    sessionString = sessionString.length > 0 ? sessionString : @"";
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"ASP.NET_SessionId=%@" , sessionString] forHTTPHeaderField:@"Cookie"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSMutableDictionary * tempDic = [NSMutableDictionary dictionary];
    [tempDic setObject:url forKey:@"key"];
    [tempDic setObject:parameter forKey:@"postData"];
    __weak typeof(self) weakSelf = self;
    [manager POST:[NSString stringWithFormat:@"%@%@" , HTMLURL , @"api/commonhandle"] parameters:@{@"dreamSoft" : tempDic} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString * responseString = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];
        NSDictionary *dict = [self jsonStringToObject:responseString];
        
        successBlock(dict);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failBlock(error);
    }];
}


+ (void)loginPostWithUrlString:(NSString *)url Parameter:(NSDictionary *)parameter Success:(NetBlock)successBlock Fail:(NetBlock)failBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //设置网络请求超时时间
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript",nil];
    NSString * sessionString = @"";
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"ASP.NET_SessionId=%@" , sessionString] forHTTPHeaderField:@"Cookie"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSMutableDictionary * tempDic = [NSMutableDictionary dictionary];
    [tempDic setObject:url forKey:@"key"];
    [tempDic setObject:parameter forKey:@"postData"];
    __weak typeof(self) weakSelf = self;
    [manager POST:[NSString stringWithFormat:@"%@%@" , HTMLURL , @"api/commonhandle"] parameters:@{@"dreamSoft" : tempDic} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString * responseString = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];
        NSDictionary *dict = [self jsonStringToObject:responseString];
        
        successBlock(dict);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failBlock(error);
    }];
}




@end
